package com.wayne.blog.controller;

import com.wayne.blog.common.Ret;
import com.wayne.blog.service.ArticleLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章喜欢控制器
 */
@RestController
@RequestMapping("/api/likes")
public class ArticleLikeController {

    @Autowired
    private ArticleLikeService articleLikeService;

    /**
     * 喜欢文章
     */
    @PostMapping("/articles/{id}")
    public Ret<?> likeArticle(@PathVariable Long id) {
        try {
            articleLikeService.likeArticle(id);
            return Ret.success();
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 取消喜欢文章
     */
    @PostMapping("/articles/{id}/cancel")
    public Ret<?> unlikeArticle(@PathVariable Long id) {
        try {
            articleLikeService.unlikeArticle(id);
            return Ret.success();
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 检查是否喜欢文章
     */
    @GetMapping("/articles/{id}")
    public Ret<?> checkLike(@PathVariable Long id) {
        try {
            boolean isLiked = articleLikeService.isLiked(id);
            return Ret.success(isLiked);
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }

    /**
     * 获取文章喜欢数
     */
    @GetMapping("/articles/{id}/count")
    public Ret<?> getLikeCount(@PathVariable Long id) {
        try {
            int count = articleLikeService.getLikeCount(id);
            return Ret.success(count);
        } catch (Exception e) {
            return Ret.error(e.getMessage());
        }
    }
} 